Skip to content

feat: add CacheReadInputTokens and CacheWriteInputTokens to TokenUsageRecord#229

Open
pawbana wants to merge 2 commits intomainfrom
pb/read-write-tokens
Open

feat: add CacheReadInputTokens and CacheWriteInputTokens to TokenUsageRecord#229
pawbana wants to merge 2 commits intomainfrom
pb/read-write-tokens

Conversation

@pawbana
Copy link
Copy Markdown
Contributor

@pawbana pawbana commented Mar 25, 2026

Added cache read and cache write input token as a first-class value.

Added CacheReadInputTokens and CacheWriteInputTokens fields to TokenUsageRecord struct.
Removed those values from ExtraTokenTypes field.

Part 1 of: #150

Copy link
Copy Markdown
Contributor Author

pawbana commented Mar 25, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@pawbana pawbana force-pushed the pb/read-write-tokens branch 2 times, most recently from eac105d to ae135f8 Compare March 25, 2026 15:13
@pawbana pawbana marked this pull request as ready for review March 25, 2026 15:16
@pawbana pawbana force-pushed the pb/read-write-tokens branch from 7c4899b to 2edd612 Compare March 25, 2026 15:38
@pawbana pawbana force-pushed the pb/read-write-tokens branch from 5fb3414 to 6480378 Compare March 30, 2026 10:41
Copy link
Copy Markdown
Contributor

@ssncferreira ssncferreira left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few questions regarding the corresponding coder changes 🦷

MsgID: completion.ID,
Input: calculateActualInputTokenUsage(lastUsage),
Output: lastUsage.CompletionTokens,
CacheReadInputTokens: lastUsage.PromptTokensDetails.CachedTokens,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No CacheWriteInputTokens?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenAI doesn't provide this information.

Comment on lines -137 to -138
"cache_creation_input": resp.Usage.CacheCreationInputTokens,
"cache_read_input": resp.Usage.CacheReadInputTokens,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this will have a corresponding PR on coder side, right? Because IIRC this is being used in the session feature to sum up the tokens. cc @dannykopping

Image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines -300 to -302
require.Equal(t, 11904.0, promtest.ToFloat64(m.TokenUseCount.WithLabelValues(config.ProviderOpenAI, "gpt-4.1", "input_cached", defaultActorID, clientLabel)))
require.Equal(t, 0.0, promtest.ToFloat64(m.TokenUseCount.WithLabelValues(config.ProviderOpenAI, "gpt-4.1", "output_reasoning", defaultActorID, clientLabel)))
require.Equal(t, 12077.0, promtest.ToFloat64(m.TokenUseCount.WithLabelValues(config.ProviderOpenAI, "gpt-4.1", "total_tokens", defaultActorID, clientLabel)))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have any grafana dashboards that use these labels?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I've summarized metric labels regarding cached tokens similar to how it is done in DB records but maybe I should keep both.
My intuition was to keep parity between DB records and metric records but it is not possible to update metrics like DB records can be migrated.
But if dashboard are not used much maybe it would be better to just change labels and move fast? I'll check if those labels are used now or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants